<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/7/16
 * Time: 9:48
 */
namespace app\order\model;

use think\Db;

class Orderexec extends BaseModel{
    protected $tableName = 'ari_orders_exec';

    /**
     * 获取上次添加的id
     * @return string
     */
    public function lastSql(){
        $lastId = Db::table($this->tableName)->getLastInsID();
        return $lastId;
    }
    /**
     * 排序输出
     * @param $where
     * @param $order
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function orderSelect($where, $order){
        $data = Db::table($this->tableName)->where($where)->order($order)->select();
        return $data;
    }

    /**
     * 分页联合查询
     * @param $where
     * @return \think\paginator\Collection
     */
    public function joinAll($where){
        $num = 10;
        $count = $this->count($where);
        $sale_id = session('order_saleid');
        $join = array(
            ['ari_model_types model', $this->tableName.'.model_id=model.model_id'],
            ['ari_manus manus', $this->tableName.'.mf_id=manus.mf_id'],
            ['ari_product_principles princ', $this->tableName.'.pid=princ.pid'],
            ['ari_orders orders', $this->tableName.'.oid=orders.oid'],
            ['ari_clients cli', 'ari_orders.cid=cli.cid'],
            ['ari_states sta', 'ari_clients.sid=sta.sid']
        );
        //表计组只能看自己负责的订单
        $status = session('order_status');
        $surname = session('order_surname');
        //只能查看自己业务部的订单
        if($status == 3){
            $where['ari_product_principles.product_principle'] = $surname;
        }
        if($sale_id != 0){
            $joins = Db::table($this->tableName)->join($join)->order('create_time_exec desc')->where(array('ari_orders.sale_id'=>$sale_id))
                ->where($where)->paginate($num, $count, ['query' => request()->param()]);
        }else{
            $joins = Db::table($this->tableName)->join($join)->order('create_time_exec desc')
                ->where($where)->paginate($num, $count, ['query' => request()->param()]);
        }
        return $joins;
    }

    /**
     * 不带分页联合查询
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function joinSelect($where){
        $join = array(
            ['ari_model_types model', $this->tableName.'.model_id = model.model_id'],
            ['ari_manus manus', $this->tableName.'.mf_id = manus.mf_id'],
            ['ari_product_principles princ', $this->tableName.'.pid = princ.pid'],
            ['ari_orders orders', $this->tableName.'.oid = orders.oid'],
            ['ari_clients cli', 'ari_orders.cid=cli.cid'],
            ['ari_states sta', 'ari_clients.sid=sta.sid']
        );
        $joins = Db::table($this->tableName)->join($join)->where($where)->select();
        return $joins;
    }
    /**
     * 不带分页联合查询,排序输出
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function joinOrder($where, $order){
        $join = array(
            ['ari_model_types model', $this->tableName.'.model_id = model.model_id'],
            ['ari_manus manus', $this->tableName.'.mf_id = manus.mf_id'],
            ['ari_product_principles princ', $this->tableName.'.pid = princ.pid'],
            ['ari_orders orders', $this->tableName.'.oid = orders.oid']
        );
        $joins = Db::table($this->tableName)->join($join)->where($where)->order($order)->select();
        return $joins;
    }

    /**
     * for gds
     * @param $where
     * @param $order
     */
    public function joinSidOrder($where, $order){
        $join = array(
            ['ari_orders orders', $this->tableName.'.oid = orders.oid'],
            ['ari_clients cli', 'cli.cid = ari_orders.cid', "LEFT"]
        );
        $joins = Db::table($this->tableName)->join($join)->where($where)->order($order)->select();
        return $joins;
    }
    /**
     * 按条件搜索-分页
     * @param $meterNum
     * @param $deliverStatus
     * @param $sid
     * @param $cid
     * @param $orderNum
     * @param $modelNum
     * @param $mfId
     */
    public function search($meterNum, $deliverStatus, $sid, $cid, $orderNum, $modelNum, $mfId){
        $where = array();
        //需要多层判断（如果为空则不执行）
        if($cid != ''){
            $where['cli.cid'] = $cid;
        }
        if($sid != ''){
            $where['cli.sid'] = $sid;
        }
        if($deliverStatus != ''){
            $where['orders.deliver_status'] = $deliverStatus;
        }
        if($mfId != ''){
            $where[$this->tableName.'.mf_id'] = $mfId;
        }
        if($meterNum != ''){
            $meterNum = trim($meterNum);
            $where['meter_start'] = ['<=', $meterNum];
            $where['meter_end'] = ['>=', $meterNum];
        }
        if($orderNum != ''){
            $where['order_num'] = ['like', "%$orderNum%"];
        }
        if($modelNum != ''){
            $where['model_num'] = ['like', "%$modelNum%"];
        }
        //表计组只能看自己负责的订单
        $status = session('order_status');
        $surname = session('order_surname');
        //只能查看自己业务部的订单
        if($status == 3){
            $where['product_principle'] = $surname;
        }
        $sale_id = session('order_saleid');
        if($sale_id != 0){
            $where['sale_id'] = $sale_id;
        }
        $num = 10;
        $join = array(
            ['ari_model_types model', $this->tableName.'.model_id = model.model_id'],
            ['ari_manus manus', $this->tableName.'.mf_id = manus.mf_id'],
            ['ari_product_principles princ', $this->tableName.'.pid = princ.pid'],
            ['ari_orders orders', $this->tableName.'.oid = orders.oid'],
            ['ari_clients cli', 'cli.cid = ari_orders.cid', "LEFT"],
            ['ari_states sta', 'ari_clients.sid=sta.sid']
        );
        $count = Db::table($this->tableName)->join($join)->where($where)->count();
        $joins = Db::table($this->tableName)->join($join)->where($where)->order('create_time_exec desc')->paginate($num, $count, ['query' => request()->param()]);
//        var_dump($joins);exit();
        return $joins;
    }

    /**
     * 按基表型号分类，for统计报表
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function graphMeter(){
        $field = 'oid_exec,sum_amounts,ari_orders.meter_id,sum_units,ari_orders.order_qty';
        $join = array(
            ['ari_orders orders', $this->tableName.'.oid = orders.oid'],
            ['ari_meter_types meter', 'ari_orders.meter_id=meter.meter_id']
        );
        $joins = Db::table($this->tableName)->join($join)->field($field)->select();
        return $joins;
    }


}